package jfconfig;

/**
 * Created : Apr 3, 2012
 *
 * @author pquiring
 */

import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.io.*;

import javaforce.*;
import javaforce.linux.*;

public class InterfaceEditDialog extends javax.swing.JDialog {

  /**
   * Creates new form InterfaceEditDialog
   */
  public InterfaceEditDialog(java.awt.Frame parent, boolean modal, InterfacePanel.Interface iface) {
    super(parent, modal);
    initComponents();
    setPosition();
    this.iface = iface;
    if (!iface.dhcp4) static4.setSelected(true);
    ip4.setText(iface.ip4);
    mask4.setText(iface.mask4);
    gw4.setText(iface.gateway4);
    if (iface.disableIP6) disableIP6.setSelected(true);
    else if (!iface.dhcp6) static6.setSelected(true);
    ip6.setText(iface.ip6);
    gw6.setText(iface.gateway6);
    setTitle("Interface " + iface.dev);
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    buttonGroup1 = new javax.swing.ButtonGroup();
    buttonGroup2 = new javax.swing.ButtonGroup();
    dhcp4 = new javax.swing.JRadioButton();
    static4 = new javax.swing.JRadioButton();
    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    gw4 = new javax.swing.JTextField();
    mask4 = new javax.swing.JTextField();
    ip4 = new javax.swing.JTextField();
    jPanel2 = new javax.swing.JPanel();
    jLabel5 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    gw6 = new javax.swing.JTextField();
    ip6 = new javax.swing.JTextField();
    accept = new javax.swing.JButton();
    cancel = new javax.swing.JButton();
    dhcp6 = new javax.swing.JRadioButton();
    static6 = new javax.swing.JRadioButton();
    disableIP6 = new javax.swing.JRadioButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Interface eth?");

    buttonGroup1.add(dhcp4);
    dhcp4.setSelected(true);
    dhcp4.setText("DHCP");

    buttonGroup1.add(static4);
    static4.setText("Static");

    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("IP4"));

    jLabel1.setText("IP");

    jLabel2.setText("Mask");

    jLabel3.setText("Gateway");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jLabel3)
          .addComponent(jLabel1)
          .addComponent(jLabel2))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(gw4)
          .addComponent(mask4)
          .addComponent(ip4, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE))
        .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel1)
          .addComponent(ip4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel2)
          .addComponent(mask4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel3)
          .addComponent(gw4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );

    jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("IP6"));

    jLabel5.setText("IP");

    jLabel7.setText("Gateway");

    gw6.setEnabled(false);

    ip6.setEnabled(false);

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jLabel7)
          .addComponent(jLabel5))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(gw6, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
          .addComponent(ip6))
        .addContainerGap())
    );
    jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel5)
          .addComponent(ip6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel7)
          .addComponent(gw6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    accept.setText("Accept");
    accept.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        acceptActionPerformed(evt);
      }
    });

    cancel.setText("Cancel");
    cancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cancelActionPerformed(evt);
      }
    });

    buttonGroup2.add(dhcp6);
    dhcp6.setText("DHCP");
    dhcp6.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(java.awt.event.ItemEvent evt) {
        dhcp6ItemStateChanged(evt);
      }
    });

    buttonGroup2.add(static6);
    static6.setText("Static");
    static6.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(java.awt.event.ItemEvent evt) {
        static6ItemStateChanged(evt);
      }
    });

    buttonGroup2.add(disableIP6);
    disableIP6.setSelected(true);
    disableIP6.setText("Disable IPv6");
    disableIP6.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(java.awt.event.ItemEvent evt) {
        disableIP6ItemStateChanged(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(0, 0, Short.MAX_VALUE)
            .addComponent(cancel)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(accept))
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(disableIP6)
              .addComponent(static4)
              .addComponent(dhcp4)
              .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addComponent(static6)
              .addComponent(dhcp6)
              .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(0, 0, Short.MAX_VALUE)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(dhcp4)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(static4)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(18, 18, 18)
        .addComponent(disableIP6)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(dhcp6)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(static6)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(accept)
          .addComponent(cancel))
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
    dispose();
  }//GEN-LAST:event_cancelActionPerformed

  private void acceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptActionPerformed
    if (!valid()) return;
    iface.dhcp4 = dhcp4.isSelected();
    iface.ip4 = ip4.getText();
    iface.mask4 = mask4.getText();
    iface.gateway4 = gw4.getText();
    iface.disableIP6 = disableIP6.isSelected();
    iface.dhcp6 = dhcp6.isSelected();
    iface.ip6 = ip6.getText();
    iface.gateway6 = gw6.getText();
    accepted = true;
    dispose();
  }//GEN-LAST:event_acceptActionPerformed

  private void disableIP6ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_disableIP6ItemStateChanged
    if (disableIP6.isSelected()) setIP6(false);
  }//GEN-LAST:event_disableIP6ItemStateChanged

  private void dhcp6ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_dhcp6ItemStateChanged
    if (dhcp6.isSelected()) setIP6(true);
  }//GEN-LAST:event_dhcp6ItemStateChanged

  private void static6ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_static6ItemStateChanged
    if (static6.isSelected()) setIP6(true);
  }//GEN-LAST:event_static6ItemStateChanged

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton accept;
  private javax.swing.ButtonGroup buttonGroup1;
  private javax.swing.ButtonGroup buttonGroup2;
  private javax.swing.JButton cancel;
  private javax.swing.JRadioButton dhcp4;
  private javax.swing.JRadioButton dhcp6;
  private javax.swing.JRadioButton disableIP6;
  private javax.swing.JTextField gw4;
  private javax.swing.JTextField gw6;
  private javax.swing.JTextField ip4;
  private javax.swing.JTextField ip6;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabel7;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JTextField mask4;
  private javax.swing.JRadioButton static4;
  private javax.swing.JRadioButton static6;
  // End of variables declaration//GEN-END:variables

  private InterfacePanel.Interface iface;

  private boolean valid() {
    //TODO
    return true;
  }

  private void setIP6(boolean state) {
    ip6.setEnabled(state);
    gw6.setEnabled(state);
  }

  private void setPosition() {
    Dimension d = getSize();
    Rectangle s = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
    setLocation(s.width/2 - d.width/2, s.height/2 - d.height/2);
  }

  public boolean accepted;

}
